load_settings;
svmthresh = 0.9;
core_labeled = getData([data_root 'clean_data/collapsed_v1.mat']);
baseline_mat_location = [trained_root 'collapsed_v1_model.mat'];
%core_labeled = getData('SR1_ITER_IMPROVED');
% load('collapsed_bg_models');

bu = core_labeled;
%% define new data
% one of our test cameras? if so, remove it from core_labeled... For now
% jsut use last camera

    % holdout cameras....
    % dataset       index       cam id
    % sr1           4           8307
    % sr2           6           1411
    % live          6           1411
    % iter          5           1411
    
holdout_cam = 9;
new_cam = core_labeled(holdout_cam,:);
core_labeled = core_labeled(setdiff(1:size(core_labeled,1), holdout_cam),:);
% holdout_bg = collapsed_bg_models(holdout_cam,:);
% core_bg = collapsed_bg_models(setdiff(1:size(collapsed_bg_models,1), holdout_cam),:);

%% get posRectExamples... This is just used to generate reasonably sized rectangles to test, based on prior examples. However, maybe replace this with two examples... a 10x10 patch and the full image size?

posRectExamples = [];
for ix = 1:size(core_labeled)
    posRectExamples = [posRectExamples; core_labeled(ix,1).ppl_rects];
    %posRectExamples = [posRectExamples; core_labeled(ix,2).ppl_rects];
    %posRectExamples = [posRectExamples; core_labeled(ix,3).ppl_rects];
end

%% load classifier



load(baseline_mat_location);

%%
for ix = 1:size(new_cam,1)
    for jx = 1:size(new_cam,2)
        if ~isempty(new_cam(ix,jx).cam_id)
        %% apply to new image, using Robert's code
        %

        fn = strcat(image_root, sprintf('%08d',new_cam(ix,jx).cam_id), '/', num2str(new_cam(ix,jx).day), '_', num2str(new_cam(ix,jx).hour, '%06d'), '.jpg');
        im = imread(fn);
        imshow(im);
        [ppl dv] = applyLinearSVMToImage(im, model.w', svmthresh, posRectExamples);
        disp(fn);
        %% Apply to new image, using my (slower) code
        % fn = strcat(image_root, sprintf('%08d',new_cam(1,1).cam_id), '/', num2str(new_cam(1,1).day), '_', num2str(new_cam(1,1).hour, '%06d'), '.jpg');
        % im = imread(fn);
        % [dvs rects] = applySVM(im, model.w); % done this way so I can tewst for best threshold without reapplying SVM
        %
        % thresh = 0.5;
        % ppl = rects(dvs > thresh);
        % bg = rects(dvs <= thresh);
        
        
        %% Visualize rectangles from applyLinear
        
%         clf;
%         figure(1);
%         imagesc(im);
%         hold on;
%         for kx = 1:size(ppl,1);
%             rectangle('Position',ppl(kx,:));
%         end
%         hold off;
%         title(['threshold: ' num2str(svmthresh)]);
%         print(['../' num2str(svmthresh) 'post_' sprintf('%08d',new_cam(ix,jx).cam_id) '_' num2str(new_cam(ix,jx).day) '_' num2str(new_cam(ix,jx).hour, '%06d') '.jpg'], '-djpeg');
% %         pause();
        %%
        end
    end
end
